// pages/modes/mode.js

import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    index: 0,
    selectAnswers: [],
    answersCard: {},
    show: false,
    color: {
      "default": "#cccccc",
      "primary": "#1989fa",
      "success": "#07c160",
      "danger": "#ee0a24"
    },
    timeVal: 100,
    timeText: 10 * 60,
    timeAll: 10 * 60,
    timeOut: false
  },

  onClickTag(idx) {
    let index = idx.currentTarget.dataset.tag
    this.setData({
      index,
      selectAnswers: this.data.answersCard[index] ? this.data.answersCard[index].split() : []
    })
  },

  onLookOptions() {
    this.setData({
      show: true
    })
  },

  onClose() {
    this.setData({
      show: false
    })
  },

  onChange(event) {
    this.setData({
      selectAnswers: event.detail
    })
  },

  onAnswersTop() {
    this.currentList(false)
    this.setData({
      index: this.data.index - 1,
    })
  },

  onAnswersBottom() {
    this.currentList(true)
    this.setData({
      index: this.data.index + 1,
    })
  },

  setCurrentAnswers(list) {
    return list.map(v => {
      return {
        ...v,
        options: JSON.parse(v.options),
        answers: JSON.parse(v.answers).join("")
      }
    })
  },

  currentList(bln) {
    let index = this.data.index
    let answersCard = this.data.answersCard
    answersCard[index] = this.data.selectAnswers.sort().join("")
    let idx = bln ? index + 1 : index - 1
    this.setData({
      answersCard,
      selectAnswers: this.data.answersCard[idx] ? this.data.answersCard[idx].split("") : []
    })
  },

  setTime() {
    let time = this.data.timeText,
      times = this.data.timeText
    this.setData({
      timeText: this.file0(Math.floor(time / 60)) + ":" + this.file0(Math.floor(time % 60))
    })
    let ttt = setInterval(() => {
      time--
      let timeText = this.file0(Math.floor(time / 60)) + ":" + this.file0(Math.floor(time % 60))
      this.setData({
        timeText,
        timeVal: Math.round((time / times) * 100)
      })
      if (time == 0 || this.data.timeOut) {
        clearInterval(ttt)
        this.getRate()
      }
    }, 1000)
  },

  file0(num) {
    return num > 9 ? num : "0" + num
  },

  getList() {
    wx.request({
      // url: 'http://192.168.43.198:2345/exam/rand-list',
      url: 'http://localhost:2345/exam/rand-list',
      data: {
        pageSize: 10
      },
      success: res => {
        this.setData({
          list: this.setCurrentAnswers(res.data.data)
        })
      }
    })
  },

  getRate() {
    let arr = [],
      obj = this.data.answersCard
    obj[this.data.list.length - 1] = this.data.selectAnswers.join("")
    this.setData({
      answersCard: obj
    })
    this.data.list.forEach((v, i) => {
      if (v.answers == this.data.answersCard[i]) {
        arr.push(true)
      } else {
        arr.push(false)
      }
    })
    let rate = arr.reduce((t, v) => {
      let item = 100 / this.data.list.length
      if (v) {
        return t += item
      } else {
        return t
      }
    }, 0)
    let timeArr = this.data.timeText.split(":"),
      timeOther = timeArr[0] * 60 + timeArr[1] * 1,
      timeAll = this.data.timeAll,
      userTime = this.file0(Math.floor((timeAll - timeOther) / 60)) + "分" + this.file0(Math.floor((timeAll - timeOther) % 60)) + "秒"
    Dialog.alert({
      title: '答题结束',
      message: `分数：${rate}分 \n 用时：${userTime}`,
    }).then(() => {
      wx.switchTab({
        url: '../exam/exam',
      })
    });
  },

  onSubmit() {
    Dialog.confirm({
        title: '提示',
        message: '是否确认交卷?',
      })
      .then(() => {
        this.setData({
          timeOut: true
        })
      })
      .catch(() => {});
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.mode == "exam") {
      this.setData({
        color: {
          "default": "#cccccc",
          "primary": "#1989fa",
          "success": "#cccccc",
          "danger": "#cccccc"
        }
      })
    }
    this.getList()
    this.setTime()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})